<template>
  <!-- 工单处理进程表单弹窗 -->
  <el-dialog
          v-model="visibleDialog"
          :title="props.title"
          width="70%"
          draggable
          @close="closeDialog"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="160">
      <el-row>
      <el-col :span="12">
      <el-form-item label="数据版本号" prop="version">
      <el-input v-if="!props.isReadonly" v-model="formData.version" placeholder="请输入数据版本号" clearable />
  
      <span v-else>{{formData.version}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="工单主键" prop="orderId">
      <el-input v-if="!props.isReadonly" v-model="formData.orderId" placeholder="请输入工单主键" clearable />
  
      <span v-else>{{formData.orderId}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="解决措施" prop="solutionMeasures">
      <el-input v-if="!props.isReadonly" v-model="formData.solutionMeasures" placeholder="请输入解决措施" clearable />
  
      <span v-else>{{formData.solutionMeasures}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="措施时间" prop="measuresTime">
      <el-input v-if="!props.isReadonly" v-model="formData.measuresTime" placeholder="请输入措施时间" clearable />
  
      <span v-else>{{formData.measuresTime}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="完成度" prop="completion">
      <el-input v-if="!props.isReadonly" v-model="formData.completion" placeholder="请输入完成度" clearable />
  
      <span v-else>{{formData.completion}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="创建时间" prop="createTime">
      <el-input v-if="!props.isReadonly" v-model="formData.createTime" placeholder="请输入创建时间" clearable />
  
      <span v-else>{{formData.createTime}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="创建人" prop="createdUserId">
      <el-input v-if="!props.isReadonly" v-model="formData.createdUserId" placeholder="请输入创建人" clearable />
  
      <span v-else>{{formData.createdUserId}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="创建人名" prop="createdUserName">
      <el-input v-if="!props.isReadonly" v-model="formData.createdUserName" placeholder="请输入创建人名" clearable />
  
      <span v-else>{{formData.createdUserName}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="更新时间" prop="lastUpdateTime">
      <el-input v-if="!props.isReadonly" v-model="formData.lastUpdateTime" placeholder="请输入更新时间" clearable />
  
      <span v-else>{{formData.lastUpdateTime}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="更新人" prop="lastUpdatedUserId">
      <el-input v-if="!props.isReadonly" v-model="formData.lastUpdatedUserId" placeholder="请输入更新人" clearable />
  
      <span v-else>{{formData.lastUpdatedUserId}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="更新人名" prop="lastUpdatedUserName">
      <el-input v-if="!props.isReadonly" v-model="formData.lastUpdatedUserName" placeholder="请输入更新人名" clearable />
  
      <span v-else>{{formData.lastUpdatedUserName}}</span>
        </el-form-item>
      </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button v-if="!props.isReadonly" type="primary" :loading="loading" @click="handleSubmit">确 定</el-button>
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
  import request from "@/utils/request";
  import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
  const dictionaryStore = useDictStoreHook();

  defineOptions({
    name: "EAMESMESMWORKORDERPROCESSADDORUPDATE",
    inheritAttrs: false,
  });

  const emit = defineEmits(["reset-table", "change-visible"]);

  const props = defineProps({
    visible: {
      type: Boolean,
      default: () => {
        return false;
      },
    },
    title: {
      type: String,
      default: () => {
        return "";
      },
    },
    data: {
      type: Object,
      default: () => {
        return {};
      },
    },
    isReadonly: {
      type: Boolean,
      default: () => {
        return false;
      },
    },
  });

  const visibleDialog = ref(false);
  watch(
      () => props.visible,
      (newVal: boolean) => {
        visibleDialog.value = newVal;
        setTimeout(() => {
          formRef.value.clearValidate();
        }, 50);
      }
  );

  const rules = reactive({
    version: [
                        ],
    orderId: [
                        ],
    solutionMeasures: [
                        ],
    measuresTime: [
                        ],
    completion: [
                        ],
    createTime: [
                        ],
    createdUserId: [
                        ],
    createdUserName: [
                        ],
    lastUpdateTime: [
                        ],
    lastUpdatedUserId: [
                        ],
    lastUpdatedUserName: [
                        ],
  });

  const formRef = ref(ElForm);

  let formData = reactive({
 id: "",  version: "",  orderId: "",  solutionMeasures: "",  measuresTime: "",  completion: "",  createTime: "",  createdUserId: "",  createdUserName: "",  lastUpdateTime: "",  lastUpdatedUserId: "",  lastUpdatedUserName: "",   });

  watch(
          () => props.data,
          (newVal: any) => {
            formData.id = ""
            if (newVal?.id) {
              const {  id,  version,  orderId,  solutionMeasures,  measuresTime,  completion,  createTime,  createdUserId,  createdUserName,  lastUpdateTime,  lastUpdatedUserId,  lastUpdatedUserName,  } = newVal;
              Object.assign(formData, {  id,  version,  orderId,  solutionMeasures,  measuresTime,  completion,  createTime,  createdUserId,  createdUserName,  lastUpdateTime,  lastUpdatedUserId,  lastUpdatedUserName,  });
            }
          }
  );

  /** 关闭表单弹窗 */
  function closeDialog() {
    visibleDialog.value = false;
    emit("change-visible", false);
    resetForm();
  }

  /** 重置表单 */
  function resetForm() {
    formRef.value.resetFields();
    formRef.value.clearValidate();
    formData.id = ""
    formData.version = ""
    formData.orderId = ""
    formData.solutionMeasures = ""
    formData.measuresTime = ""
    formData.completion = ""
    formData.createTime = ""
    formData.createdUserId = ""
    formData.createdUserName = ""
    formData.lastUpdateTime = ""
    formData.lastUpdatedUserId = ""
    formData.lastUpdatedUserName = ""
  }

  const loading = ref(false);

  /** 工单处理进程保存提交 */
  function handleSubmit() {
    formRef.value.validate(async (valid: any) => {
      if (valid) {
        loading.value = true;
        const text = formData.id ? "编辑" : "新增";
        await request({
          url: "/eam/esmworkorderprocess/save",
          method: "post",
          data: formData,
        }).finally(() => {
          loading.value = false;
        });
        ElMessage.success( text +`成功`);
        closeDialog();
        emit("reset-table", formData.id);
      }
    });
  }


  // 组件销毁时调用
  onBeforeUnmount(() => {
    visibleDialog.value = false;
    emit("change-visible", false);
  });


</script>
